<?php
class Collect extends Eloquent {

    const TYPE_POST = 1;
    const TYPE_USER = 2;
    const TYPE_ARTICLE = 3;

    public static function collect_post(Post $post)
    {
        $collect = new Collect();
        $collect->collect_type = self::TYPE_POST;
        $collect->item_id = $post->id;
        $collect->creator_id = Auth::user()->id;

        DB::transaction(function() use($post, $collect){
            $collect->save();
            $post_extra = $post->postExtra()->first();
			$post_extra->increment('collects_count');
			$post_extra->touch();
			$post_extra->save();
        });
    }

    public static function collect_user(User $user)
    {
        $collect = new Collect();
        $collect->collect_type = self::TYPE_USER;
        $collect->item_id = $user->id;
        $collect->creator_id = Auth::user()->id;

        DB::transaction(function() use($user, $collect){
            $collect->save();
            $user->increment('collects_count');
			$user->touch();
			$user->save();
        });
    }

    public static function collect_article(Article $article)
    {
        $collect = new Collect();
        $collect->collect_type = self::TYPE_ARTICLE;
        
        
        $collect->item_id = $article->id;
        $collect->creator_id = Auth::user()->id;

        DB::transaction(function() use($article, $collect){

        	
            $collect->save();
            $article->increment('collects_count');
			$article->touch();

			$article->save();

        });
    }
    
    public static function delete_collect_post($collect)
    {
    	DB::transaction(function() use($collect){
    		$collect->delete();
//     		$post = Post::find(Input::get('item_id'));
//     		$post->decrement('collects_count');
//     		$post->touch();
//     		$post->save(); 
    	});
    }
    public static function delete_collect_user($collect)
    {
    	DB::transaction(function() use($collect){
    		$collect->delete();
    		$user = User::find(Input::get('item_id'));
    		$user->decrement('collects_count');
    		$user->touch();
    		$user->save();
    		 
    	});
    }    
    public static function delete_collect_article($collect)
    {
    	DB::transaction(function() use($collect){

    		$article = Article::find($collect->item_id);
    		$collect->delete();
    		$article->decrement('collects_count');
    		$article->touch();
    		$article->save();
    	
    	});
    }
    
    public static function exists($collect_type, $item_id){
    	$result = Collect::where('collect_type', $collect_type)
    						->where('item_id', $item_id)
    						->where('creator_id', Auth::user()->id)
    						->first();
    	return (bool)$result;
    }
    
    

    public static function collected_post(Post $post)
    {
        return Collect::where('collect_type', self::TYPE_POST)->OfItemId($post->id)->OfCreatorId(Auth::user()->id)->count() > 0;
    }

    public static function collected_user(User $user)
    {
       return Collect::where('collect_type', self::TYPE_USER)->OfItemId($user->id)->OfCreatorId(Auth::user()->id)->count() > 0;
    }
    public static function collected_article(Article $article)
    {
    	return Collect::where('collect_type', self::TYPE_ARTICLE)->OfItemId($article->id)->OfCreatorId(Auth::user()->id)->count() > 0;
    }

    public function scopeOfItemId($query, $item_id)
    {
        return $query->whereItemId($item_id);
    }

    public function scopeOfCollectType($query, $collect_type)
    {
        return $query->whereCollectType($collect_type);
    }
    
    public function scopeOfCreatorId($query, $creator_id)
    {
        return $query->whereCreatorId($creator_id);
    }
		
    public static function getLawyers($count, $offset = NULL, $limit = NULL, $order_by = 'collects.created_at')
    {
			 if($count)
			 {
					return Collect::where('collect_type', self::TYPE_USER)->OfCreatorId(Auth::user()->id)->count();
			 } else {
					return Collect::where('collect_type', self::TYPE_USER)
						->leftJoin('lawyer', 'lawyer.user_id', '=', 'collects.item_id')	
						->leftJoin('user', 'user.id', '=', 'lawyer.user_id')
						->OfCreatorId(Auth::user()->id)
						->take($limit)
						->skip($offset)
						->orderBy($order_by, 'DESC')
						->get();
			 }
    }
		
    public static function getPosts($count, $offset = NULL, $limit = NULL, $order_by = 'collects.created_at')
    {
			 if($count)
			 {
					return Collect::where('collect_type', self::TYPE_POST)->OfCreatorId(Auth::user()->id)->count();
			 } else {
					return Collect::where('collect_type', self::TYPE_POST)->OfCreatorId(Auth::user()->id)->take($limit)->skip($offset)->orderBy($order_by, 'DESC')->get();
			 }
    }
}